﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CIS470MetricsTracking
{
	public partial class ProjectSelection : Form
	{
		private UserLogin login_form;

		public ProjectSelection(UserLogin login_form)
		{
			InitializeComponent();

			this.login_form = login_form;
		}

		private void btnCreateProject_Click(object sender, EventArgs e)
		{
			CreateProject create_project = new CreateProject(this);
			create_project.Show();
			create_project.BringToFront();
			this.Hide();
		}

		private void ProjectSelection_Load(object sender, EventArgs e)
		{
			var projects = ControllerManager.project_controller.getProjects();

			if (projects.Count != 0) {
				foreach (var k in projects.Keys) {
					lstProjectSelect.Items.Add(k.ToString());
				}
			}

			if (ControllerManager.user_controller.getUserAccessLevel() != 1) {
				btnCreateProject.Enabled = false;
				btnManageProject.Enabled = false;
			}
		}

		private void lstProjectSelect_SelectedIndexChanged(object sender, EventArgs e)
		{
			if (lstProjectSelect.SelectedItem != null) {
				var proj = ControllerManager.project_controller.getProject(lstProjectSelect.SelectedItem.ToString());
				txtProjectName.Text = proj.getProjectName();
				txtStartDate.Text = proj.getProjectStart();
				txtEndDate.Text = proj.getProjectEnd();
				txtDescription.Text = proj.getProjectDesc();
				txtReportPeriod.Text = ControllerManager.project_controller.calculate_report_period(proj.getProjectStart()).ToString();
			}
		}

		private void btnViewProject_Click(object sender, EventArgs e)
		{
			if (lstProjectSelect.SelectedItem != null) {
				var proj = ControllerManager.project_controller.getProject(lstProjectSelect.SelectedItem.ToString());
				ProjectManagement proj_mgmt = new ProjectManagement(this, proj);
				proj_mgmt.Show();
				proj_mgmt.BringToFront();
				this.Hide();
			} else {
				MessageBox.Show("Must select a project to perform this function.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
			}
		}

		private void btnManageProject_Click(object sender, EventArgs e)
		{
			if (lstProjectSelect.SelectedItem != null) {
				var proj = ControllerManager.project_controller.getProject(lstProjectSelect.SelectedItem.ToString());
				ProjectManagement proj_mgmt = new ProjectManagement(this, proj);
				proj_mgmt.Show();
				proj_mgmt.BringToFront();
				this.Hide();
			} else {
				MessageBox.Show("Must select a project to perform this function.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
			}
		}

		private void btnLogOut_Click(object sender, EventArgs e)
		{
			ControllerManager.user_controller.log_out();
			login_form.Show();
			login_form.BringToFront();
			this.Close();
		}

		private void ProjectSelection_FormClosing(object sender, FormClosingEventArgs e)
		{
			ControllerManager.user_controller.log_out();
			login_form.Show();
			login_form.BringToFront();
		}

		private void ProjectSelection_Shown(object sender, EventArgs e)
		{

		}

		private void ProjectSelection_VisibleChanged(object sender, EventArgs e)
		{
			if (lstProjectSelect.Items.Count != 0) {
				lstProjectSelect.Items.Clear();
				lstProjectSelect.ClearSelected();
				txtDescription.Text = "";
				txtEndDate.Text = "";
				txtProjectName.Text = "";
				txtReportPeriod.Text = "";
				txtStartDate.Text = "";
			}

			var projects = ControllerManager.project_controller.getProjects();

			if (projects.Count != 0) {
				foreach (var k in projects.Keys) {
					lstProjectSelect.Items.Add(k.ToString());
				}
			}

			if (ControllerManager.user_controller.getUserAccessLevel() != 1) {
				btnCreateProject.Enabled = false;
				btnManageProject.Enabled = false;
			}	
		}
	}
}
